#############################################################################
#Yuhua Wang#
#The Rise and Fall of Imperial China: The Social Origins of State Development#
#Princeton University Press, 2022#
############################################################################

############################################################################
#Appendices#
############################################################################

######################################
#Table A.1: Temperature Anomalies and Conflict: OLS Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure B.1: Major Officials’ Marriage Network and Communities under Emperor Zhenzong (997–1022)#
######################################

rm(list = ls())
library(statnet)
library("network")

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")

set.seed(12349)

#Read in Song Officials' Adjacency Matrix

net.adj.song<-read.csv(file="all song officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)

net.adj.song <- as.matrix(net.adj.song) 

# load in vertex attributes
nodeInfosong <- read.csv(file="all song officials vertex attributes.csv",header=TRUE,stringsAsFactors=FALSE)

print(net.adj.song) # peek at matrix 
print(nodeInfosong)  # peek at attribute data

# Since our relational data has no row/column names, let's set them now
rownames(net.adj.song) <- nodeInfosong$name_pinyin
colnames(net.adj.song) <- nodeInfosong$name_pinyin

print(net.adj.song)

# create undirected network object from matrix

net_official_song<-network(net.adj.song, directed=FALSE, matrix.type="adjacency")

# it read in vertex names from matrix col names ...
network.vertex.names(net_official_song)

# ATTACHING VERTEX ATTRIBUTES

# ... but could also set vertex.names with 
net_official_song%v%'vertex.names'<- nodeInfosong$name_pinyin


net_official_song%v%"reign1" <- nodeInfosong$reign1
net_official_song%v%"reign2" <- nodeInfosong$reign2
net_official_song%v%"reign3" <- nodeInfosong$reign3
net_official_song%v%"reign4" <- nodeInfosong$reign4
net_official_song%v%"reign5" <- nodeInfosong$reign5
net_official_song%v%"reign6" <- nodeInfosong$reign6
net_official_song%v%"reign7" <- nodeInfosong$reign7
net_official_song%v%"reign8" <- nodeInfosong$reign8
net_official_song%v%"reign9" <- nodeInfosong$reign9
net_official_song%v%"reign10" <- nodeInfosong$reign10
net_official_song%v%"reign11" <- nodeInfosong$reign11
net_official_song%v%"reign12" <- nodeInfosong$reign12
net_official_song%v%"reign13" <- nodeInfosong$reign13
net_official_song%v%"reign14" <- nodeInfosong$reign14
net_official_song%v%"reign15" <- nodeInfosong$reign15
net_official_song%v%"reign16" <- nodeInfosong$reign16
net_official_song%v%"reign17" <- nodeInfosong$reign17


# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex

# check that they got loaded
list.vertex.attributes(net_official_song)

summary(net_official_song)

gden(net_official_song)

#0.004011799

# graphs

rescale<-function(nchar,low,high){
  min_d<-min(nchar)
  max_d<-max(nchar)
  rscl<-((high-low)*(nchar-min_d))/(max_d-min_d)+low
  rscl
}


#Network graph

#(a) Major Officials’ Marriage Network#
#reign3	"Zhenzong"

net_songofficial_reign3<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'reign3'==1))

summary(net_songofficial_reign3)
gden_net_songofficial_reign3 <- data.frame(table(gden(net_songofficial_reign3)))
gden(net_songofficial_reign3)

deg_song_r3<-degree(net_songofficial_reign3,gmode="graph")
deg_song_r3

op<-par(mar=c(0,0,0,0))
plot(net_songofficial_reign3,vertex.col="#DF536B", vertex.cex=rescale(deg_song_r3,0.5,3), edge.col="grey80", mode="fruchtermanreingold")
par(op)


#(b) Communities in the Marriage Network#

library(igraph)
library(intergraph)

net_songofficial_reign3_ig <- asIgraph(net_songofficial_reign3)

deg_song_ir3 <- igraph::degree(net_songofficial_reign3_ig, mode = "all")
deg_song_ir3

com_r3 <- cluster_edge_betweenness(net_songofficial_reign3_ig)
com_r3
membership(com_r3)
table(membership(com_r3))
com_r3.data<-data.frame(table(membership(com_r3)))
com_r3.data

plot(com_r3, net_songofficial_reign3_ig, vertex.label=NA)

######################################
#Figure B.2: Social Fractionalization of Major Officials’ Marriage Networks in Song (960–1279)#
######################################

rm(list = ls())
library(statnet)
library("network")

set.seed(12346)

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")

#Network among all Song politicians

#Read in Song Officials' Adjacency Matrix

net.adj.song<-read.csv(file="all song officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)

net.adj.song <- as.matrix(net.adj.song) 

# load in vertex attributes
nodeInfosong <- read.csv(file="all song officials vertex attributes.csv",header=TRUE,stringsAsFactors=FALSE)

print(net.adj.song) # peek at matrix 
print(nodeInfosong)  # peek at attribute data

# Since our relational data has no row/column names, let's set them now
rownames(net.adj.song) <- nodeInfosong$name_pinyin
colnames(net.adj.song) <- nodeInfosong$name_pinyin

print(net.adj.song)

# create undirected network object from matrix

net_official_song<-network(net.adj.song, directed=FALSE, matrix.type="adjacency")

# it read in vertex names from matrix col names ...
network.vertex.names(net_official_song)

# ATTACHING VERTEX ATTRIBUTES

# ... but could also set vertex.names with 
net_official_song%v%'vertex.names'<- nodeInfosong$name_pinyin

net_official_song%v%"cohort1" <- nodeInfosong$cohort1
net_official_song%v%"cohort2" <- nodeInfosong$cohort2
net_official_song%v%"cohort3" <- nodeInfosong$cohort3
net_official_song%v%"cohort4" <- nodeInfosong$cohort4
net_official_song%v%"cohort5" <- nodeInfosong$cohort5
net_official_song%v%"cohort6" <- nodeInfosong$cohort6
net_official_song%v%"cohort7" <- nodeInfosong$cohort7
net_official_song%v%"cohort8" <- nodeInfosong$cohort8
net_official_song%v%"cohort9" <- nodeInfosong$cohort9
net_official_song%v%"cohort10" <- nodeInfosong$cohort10
net_official_song%v%"cohort11" <- nodeInfosong$cohort11
net_official_song%v%"cohort12" <- nodeInfosong$cohort12
net_official_song%v%"cohort13" <- nodeInfosong$cohort13
net_official_song%v%"cohort14" <- nodeInfosong$cohort14
net_official_song%v%"cohort15" <- nodeInfosong$cohort15
net_official_song%v%"cohort16" <- nodeInfosong$cohort16

# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex

# check that they got loaded
list.vertex.attributes(net_official_song)

summary(net_official_song)

gden(net_official_song)

#0.004011799


#Network by cohort
 
#cohort1 pre-980#
net_songofficial_cohort1<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort1'==1))

#cohort2 980-999#
net_songofficial_cohort2<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort2'==1))

#cohort3 1000-1019#
net_songofficial_cohort3<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort3'==1))

#cohort4 1020-1039#
net_songofficial_cohort4<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort4'==1))

#cohort5 1040-1059#
net_songofficial_cohort5<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort5'==1))

#cohort6 1060-1079#
net_songofficial_cohort6<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort6'==1))

#cohort7 1080-1099#
net_songofficial_cohort7<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort7'==1))

#cohort8 1100-1119#
net_songofficial_cohort8<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort8'==1))

#cohort9 1120-1139#
net_songofficial_cohort9<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort9'==1))

#cohort10 1140-1159#
net_songofficial_cohort10<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort10'==1))

#cohort11 1160-1179#
net_songofficial_cohort11<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort11'==1))

#cohort12 1180-1199#
net_songofficial_cohort12<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort12'==1))

#cohort13 1200-1219#
net_songofficial_cohort13<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort13'==1))

#cohort14 1220-1239#
net_songofficial_cohort14<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort14'==1))

#cohort15 1240-1259#
net_songofficial_cohort15<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort15'==1))

#cohort16 post-1260#
net_songofficial_cohort16<-get.inducedSubgraph(net_official_song,eid=which(net_official_song%v%'cohort16'==1))

#Community detection by cohort

library(igraph)
library(intergraph)

#cohort1
net_songofficial_cohort1_ig <- asIgraph(net_songofficial_cohort1)

deg_song_ic1 <- igraph::degree(net_songofficial_cohort1_ig, mode = "all")
deg_song_ic1

com_c1 <- cluster_edge_betweenness(net_songofficial_cohort1_ig)
com_c1
membership(com_c1)
table(membership(com_c1))
com_c1.data<-data.frame(table(membership(com_c1)))
com_c1.data
write.csv(com_c1.data,file="com_c1.csv")

#cohort2
net_songofficial_cohort2_ig <- asIgraph(net_songofficial_cohort2)

deg_song_ic2 <- igraph::degree(net_songofficial_cohort2_ig, mode = "all")
deg_song_ic2

com_c2 <- cluster_edge_betweenness(net_songofficial_cohort2_ig)
com_c2
membership(com_c2)
table(membership(com_c2))
com_c2.data<-data.frame(table(membership(com_c2)))
com_c2.data
write.csv(com_c2.data,file="com_c2.csv")

#cohort3
net_songofficial_cohort3_ig <- asIgraph(net_songofficial_cohort3)

deg_song_ic3 <- igraph::degree(net_songofficial_cohort3_ig, mode = "all")
deg_song_ic3

com_c3 <- cluster_edge_betweenness(net_songofficial_cohort3_ig)
com_c3
membership(com_c3)
table(membership(com_c3))
com_c3.data<-data.frame(table(membership(com_c3)))
com_c3.data
write.csv(com_c3.data,file="com_c3.csv")

#cohort4
net_songofficial_cohort4_ig <- asIgraph(net_songofficial_cohort4)

deg_song_ic4 <- igraph::degree(net_songofficial_cohort4_ig, mode = "all")
deg_song_ic4

com_c4 <- cluster_edge_betweenness(net_songofficial_cohort4_ig)
com_c4
membership(com_c4)
table(membership(com_c4))
com_c4.data<-data.frame(table(membership(com_c4)))
com_c4.data
write.csv(com_c4.data,file="com_c4.csv")

#cohort5
net_songofficial_cohort5_ig <- asIgraph(net_songofficial_cohort5)

deg_song_ic5 <- igraph::degree(net_songofficial_cohort5_ig, mode = "all")
deg_song_ic5

com_c5 <- cluster_edge_betweenness(net_songofficial_cohort5_ig)
com_c5
membership(com_c5)
table(membership(com_c5))
com_c5.data<-data.frame(table(membership(com_c5)))
com_c5.data
write.csv(com_c5.data,file="com_c5.csv")

#cohort6
net_songofficial_cohort6_ig <- asIgraph(net_songofficial_cohort6)

deg_song_ic6 <- igraph::degree(net_songofficial_cohort6_ig, mode = "all")
deg_song_ic6

com_c6 <- cluster_edge_betweenness(net_songofficial_cohort6_ig)
com_c6
membership(com_c6)
table(membership(com_c6))
com_c6.data<-data.frame(table(membership(com_c6)))
com_c6.data
write.csv(com_c6.data,file="com_c6.csv")

#cohort7
net_songofficial_cohort7_ig <- asIgraph(net_songofficial_cohort7)

deg_song_ic7 <- igraph::degree(net_songofficial_cohort7_ig, mode = "all")
deg_song_ic7

com_c7 <- cluster_edge_betweenness(net_songofficial_cohort7_ig)
com_c7
membership(com_c7)
table(membership(com_c7))
com_c7.data<-data.frame(table(membership(com_c7)))
com_c7.data
write.csv(com_c7.data,file="com_c7.csv")

#cohort8
net_songofficial_cohort8_ig <- asIgraph(net_songofficial_cohort8)

deg_song_ic8 <- igraph::degree(net_songofficial_cohort8_ig, mode = "all")
deg_song_ic8

com_c8 <- cluster_edge_betweenness(net_songofficial_cohort8_ig)
com_c8
membership(com_c8)
table(membership(com_c8))
com_c8.data<-data.frame(table(membership(com_c8)))
com_c8.data
write.csv(com_c8.data,file="com_c8.csv")

#cohort9
net_songofficial_cohort9_ig <- asIgraph(net_songofficial_cohort9)

deg_song_ic9 <- igraph::degree(net_songofficial_cohort9_ig, mode = "all")
deg_song_ic9

com_c9 <- cluster_edge_betweenness(net_songofficial_cohort9_ig)
com_c9
membership(com_c9)
table(membership(com_c9))
com_c9.data<-data.frame(table(membership(com_c9)))
com_c9.data
write.csv(com_c9.data,file="com_c9.csv")

#cohort10
net_songofficial_cohort10_ig <- asIgraph(net_songofficial_cohort10)

deg_song_ic10 <- igraph::degree(net_songofficial_cohort10_ig, mode = "all")
deg_song_ic10

com_c10 <- cluster_edge_betweenness(net_songofficial_cohort10_ig)
com_c10
membership(com_c10)
table(membership(com_c10))
com_c10.data<-data.frame(table(membership(com_c10)))
com_c10.data
write.csv(com_c10.data,file="com_c10.csv")

#cohort11
net_songofficial_cohort11_ig <- asIgraph(net_songofficial_cohort11)

deg_song_ic11 <- igraph::degree(net_songofficial_cohort11_ig, mode = "all")
deg_song_ic11

com_c11 <- cluster_edge_betweenness(net_songofficial_cohort11_ig)
com_c11
membership(com_c11)
table(membership(com_c11))
com_c11.data<-data.frame(table(membership(com_c11)))
com_c11.data
write.csv(com_c11.data,file="com_c11.csv")

#cohort12
net_songofficial_cohort12_ig <- asIgraph(net_songofficial_cohort12)

deg_song_ic12 <- igraph::degree(net_songofficial_cohort12_ig, mode = "all")
deg_song_ic12

com_c12 <- cluster_edge_betweenness(net_songofficial_cohort12_ig)
com_c12
membership(com_c12)
table(membership(com_c12))
com_c12.data<-data.frame(table(membership(com_c12)))
com_c12.data
write.csv(com_c12.data,file="com_c12.csv")

#cohort13
net_songofficial_cohort13_ig <- asIgraph(net_songofficial_cohort13)

deg_song_ic13 <- igraph::degree(net_songofficial_cohort13_ig, mode = "all")
deg_song_ic13

com_c13 <- cluster_edge_betweenness(net_songofficial_cohort13_ig)
com_c13
membership(com_c13)
table(membership(com_c13))
com_c13.data<-data.frame(table(membership(com_c13)))
com_c13.data
write.csv(com_c13.data,file="com_c13.csv")

#cohort14
net_songofficial_cohort14_ig <- asIgraph(net_songofficial_cohort14)

deg_song_ic14 <- igraph::degree(net_songofficial_cohort14_ig, mode = "all")
deg_song_ic14

com_c14 <- cluster_edge_betweenness(net_songofficial_cohort14_ig)
com_c14
membership(com_c14)
table(membership(com_c14))
com_c14.data<-data.frame(table(membership(com_c14)))
com_c14.data
write.csv(com_c14.data,file="com_c14.csv")

#cohort15
net_songofficial_cohort15_ig <- asIgraph(net_songofficial_cohort15)

deg_song_ic15 <- igraph::degree(net_songofficial_cohort15_ig, mode = "all")
deg_song_ic15

com_c15 <- cluster_edge_betweenness(net_songofficial_cohort15_ig)
com_c15
membership(com_c15)
table(membership(com_c15))
com_c15.data<-data.frame(table(membership(com_c15)))
com_c15.data
write.csv(com_c15.data,file="com_c15.csv")

#cohort16
net_songofficial_cohort16_ig <- asIgraph(net_songofficial_cohort16)

deg_song_ic16 <- igraph::degree(net_songofficial_cohort16_ig, mode = "all")
deg_song_ic16

com_c16 <- cluster_edge_betweenness(net_songofficial_cohort16_ig)
com_c16
membership(com_c16)
table(membership(com_c16))
com_c16.data<-data.frame(table(membership(com_c16)))
com_c16.data
write.csv(com_c16.data,file="com_c16.csv")


#calculate each cohort's social fractionalization in Stata and generate a data file "fractionalization by cohort.dta"
#Below is Stata command#

#  forvalues i = 1/16 {
#    import delimited "com_c`i'.csv", clear
#    keep var1 freq
#    egen sum=sum(freq)
#    egen com=max(var1)
#    gen frac=(freq/sum)^2
#    egen frac_sum=sum(frac)
#    gen sf=1-frac_sum
#    keep if var1==1
#    keep sf
#    gen cohort=`i'
#save "cohort`i'.dta", replace
#}

#cd "/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/

#use "cohort1.dta", clear
#forvalues p = 2/16 {
#	append using "cohort`p'.dta"
#	}

#rename sf factionalization

#save "factionalization by cohort.dta", replace

#clear


#Making Figure B.2#

rm(list = ls())
library(foreign)
library(tidyverse)
library(ggrepel) 
library(plyr)
require(scales)
require(gridExtra)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")

frac <- read.dta("fractionalization by cohort.dta")

attach(frac)

ggplot(data=frac, aes(x=start_year, y=factionalization, color="grey",linetype="solid")) + 
  geom_point() + geom_line() +
  scale_x_continuous(name="Year of Becoming Major Official", limits=c(960,1260), breaks=c(960,1000,1040,1080,1120,1160,1200,1240)) +
  scale_y_continuous(name="Social Fractionalization of Officials' Marriage Network",limits=c(0,1), breaks=c(0,0.5,1)) +
  geom_vline(xintercept = 1127,color="black",linetype="dashed")+ 
  annotate(geom="text", x = 1040, y=0.6, label = "Northern Song",size=4)+
  annotate(geom="text", x = 1200, y=0.6, label = "Southern Song", size=4)+
  theme_bw()+
  theme(legend.position="none")+
  theme(
    axis.title.y = element_text(size = 8)
  )

######################################
#Table C.1: Summary Statistics for Dataset in Chapter 5#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Table C.2: Political Selection and Geography of Kinship Network: OLS Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure C.1: Northern Song Politicians Marriage Network (1167–1185)#
######################################

rm(list = ls())
library(statnet)
library("network")
library(igraph)

set.seed(12345)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")

#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix

net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)

net.adj.child3 <- as.matrix(net.adj.child3) 

# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)

print(net.adj.child3) # peek at matrix 
print(nodeInfoCHILD3)  # peek at attribute data

# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin

print(net.adj.child3)

# create undirected network object from matrix

net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")

# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)

# ATTACHING VERTEX ATTRIBUTES

# ... but could also set vertex.names with 
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin

# load in other attributes 
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri

# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex

# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)

summary(net_reformofficial_child3)

gden(net_reformofficial_child3)

# make my color palette

library(RColorBrewer) 

addalpha <- function(colors, alpha=1.0) {
  r <- col2rgb(colors, alpha=T)
  # Apply alpha
  r[4,] <- alpha*255
  r <- r/255.0
  return(rgb(r[1,], r[2,], r[3,], r[4,]))
}

display.brewer.all()
display.brewer.pal(3, "RdYlGn")

my_pal<-brewer.pal(3,"RdYlGn") 

detach("package:igraph", unload=TRUE)

# assign different colors to supports and opponents

rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))

# centrality measures

#Degree centrality#
deg3_child<-degree(net_reformofficial_child3,gmode="graph")
deg3_child

deg3_child.data<-data.frame(deg3_child)
deg3_child.data

#Betweenness centrality#
btw3_child<-betweenness(net_reformofficial_child3,gmode="graph")
btw3_child

btw3_child.data<-data.frame(btw3_child)
btw3_child.data

#Bonacich centrality#
bonacich3_child<-bonpow(net_reformofficial_child3,gmode="graph")
bonacich3_child

bonacich3_child.data<-data.frame(bonacich3_child)
bonacich3_child.data

# Network graph

rolelab_child3<-get.vertex.attribute(net_reformofficial_child3,"support")

library(igraph)
library(gcookbook)


rescale <- function(nchar,low,high) {
  min_d <- min(nchar)
  max_d <- max(nchar)
  rscl <- ((high-low)*(nchar-min_d))/(max_d-min_d)+low 
  rscl}

op<-par(mar=c(0,0,0,0))
gplot(net_reformofficial_child3, gmode="graph", mode='fruchtermanreingold', displaylabels=F,
      pad=0.4, vertex.cex=rescale(btw3_child,1,4), edge.col="grey90",
      vertex.col=addalpha(my_pal[rolecat_child3], 0.4))
legend("topleft",legend=c("Oppose","Unkown","Support"),
       col=my_pal,pch=19,pt.cex=1.5,bty="n")
par(op)

######################################
#Table C.3: Local Concentration of Kin and Support for Reform: OLS Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Table D.1: Summary Statistics for Dataset in Chapter 6#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#


######################################
#Table D.1: Summary Statistics for Dataset in Chapter 6#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure D.1: Number of Years Taken to Implement the Single Whip#
######################################

#Constructed in QGIS

######################################
#Figure D.2: Estimated Survival and Hazard Functions of Prefectures With and Without At Least One Major Official#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure D.3: Number of Advanced Scholars and Its Correlation with the Number of Major Officials#
######################################

#(a) Number of Advanced Scholars (1368–1572)

#Constructed in QGIS

#(b) Scatterplot of Number of Major Officials

rm(list = ls())
library(foreign) 
library(ggplot2) 
require(ggplot2)
library(ggrepel) 
library(plyr)
require(scales)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")

singlewhip <- read.csv(file="Single Whip Reform data.csv", header=TRUE, sep=",")

attach(singlewhip)

ggplot(singlewhip, aes(x=presentedscholar_pre1573, y=politician)) + 
  geom_point(shape=19, color="violetred4", size=3)+
  geom_smooth(method=lm,  se=TRUE,linetype="solid",
              color="deepskyblue4", fill="deepskyblue2")+
  scale_y_continuous(name="N of Major Officials, 1573-1620",limits=c(0,20), breaks=c(0,5,10,15,20))+
  scale_x_continuous(name="N of Advanced Scholars, 1368-1572", limits=c(0,600), breaks=c(0,50,100,150,200,250,300,350,400,450,500,550,600))+
  theme(text = element_text(size=15, 
                            vjust=1, family="Times"))+
  theme_bw()

######################################
#Table D.3: National Representation and Delay in Adopting the Single Whip: Survival Analysis#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Table D.4: Advanced Scholars and Major Officials: OLS Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure E.1: Number of Lineage Organizations (1801–1850)
######################################

#Constructed in QGIS

######################################
#Figure E.2: Number of Genealogy Books (1801–1850)
######################################

#Constructed in QGIS

######################################
#Figure E.3: Number of Advanced Scholars (1644–1800)
######################################

#Constructed in QGIS

######################################
#Figure E.4: Number of Conflicts (1644–1800)
######################################

#Constructed in QGIS

######################################
#Table E.1: Summary Statistics for Dataset in Chapter 7#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Table E.2: Exam Success, Violence, and Lineage Organizations: OLS Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure F.1: Mass Rebellion Locations (1850–1869)
######################################

#Constructed in QGIS

######################################
#Table F.1: Summary Statistics for Dataset in Chapter 8#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Figure F.2: Number of Genealogy Books (1890–1909)
######################################

#Constructed in QGIS

######################################
#Table F.2: Mass Rebellion and Lineage Activity: Difference-in-Differences Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

######################################
#Table F.3: Lineage Activity and Declaration of Independence: OLS Estimates#
######################################

#Conducted in Stata#
#See Wang 2022 PUP Appendices replication.do#

